package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ledim.bean.LedimAlbumBean;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.bean.LedimPlayCountBean;
import com.ledim.bean.base.BaseResultDataInfo;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.LedimFunctionUtils;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import com.letv.android.young.client.R;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class VideoTitleView extends LinearLayout implements View.OnClickListener {

    private View mIntroduceLayout;
    private TextView mTitle;
    private TextView mNum;
    private ImageView mMore;
    private LedimAlbumBean album;
    private TextView mIntroduce;
    private TextView mIntroduceLong;
    private Context mContext;

    public VideoTitleView(Context context) {
        super(context);
        mContext = context;
    }

    public VideoTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public VideoTitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        init();
    }

    //绑定数据
    public void bindData(LedimAlbumBean album) {
        this.album = album;
        if (album.nameCn != null && album.nameCn.length() > 0) {
            mTitle.setText(album.nameCn);
        }
        if (album.description != null && album.description.length() > 0) {
            mIntroduce.setText(album.description);
            mIntroduceLong.setVisibility(View.GONE);
            mIntroduceLong.setText(album.description);
        }
        setMoreIntroduce();
    }

    //设置更多简介
    private void setMoreIntroduce() {
        ViewTreeObserver vto = mIntroduce.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (mIntroduce.getLineCount() == 1) {
                    if (mContext != null) {
                        LinearLayout.LayoutParams lp =
                                new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp.setMargins(DensityUtil.dip2px(mContext, 10), 2, DensityUtil.dip2px(mContext, 10), DensityUtil
                                .dip2px(mContext, 1));
                        mIntroduce.setLayoutParams(lp);
                    }
                    mIntroduceLayout.setVisibility(View.GONE);
                } else {
                    mIntroduce.setMaxLines(1);
                    mIntroduceLayout.setVisibility(View.VISIBLE);
                }

                return true;
            }
        });
    }

    public void setDescription(LedimEpisodeBean episode) {
        if (episode != null) {
            LogUtil.e("ccx setDescription==== getPlayCounts");
            if (episode.description != null && episode.description.length() > 0) {
                mIntroduce.setText(episode.description);
                mIntroduceLong.setText(episode.description);
            }
            setMoreIntroduce();
            getPlayCounts(album.id, episode.id + "");

        }
    }

    //绑定数据
    public void bindDataZero(String title, String vid) {
        if (!TextUtils.isEmpty(title)) {
            mTitle.setText(title);
            mIntroduce.setText(("描述:" + title));
            mIntroduceLong.setText(("描述:" + title));
        }
        setMoreIntroduce();
        getPlayCounts("0", vid);
    }

    /**
     * 初始化界面
     */
    private void init() {
        mIntroduceLayout = findViewById(R.id.video_introduce_layout);
        mTitle = (TextView) findViewById(R.id.video_title);
        mNum = (TextView) findViewById(R.id.video_num);
        mIntroduce = (TextView) findViewById(R.id.video_introduce);
        mIntroduceLong = (TextView) findViewById(R.id.video_introduce_long);
        mMore = (ImageView) findViewById(R.id.video_more_introduce);

        mIntroduceLayout.setOnClickListener(this);

        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int mWidth = wm.getDefaultDisplay().getWidth();
        mTitle.setMaxWidth(mWidth / 3 * 2);

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_introduce_layout:
                if (mIntroduce.getVisibility() == View.VISIBLE) {
                    mIntroduce.setVisibility(View.GONE);
                    mIntroduceLong.setVisibility(View.VISIBLE);
                    mMore.setImageResource(R.drawable.drop_up);
                } else {
                    mIntroduce.setVisibility(View.VISIBLE);
                    mIntroduceLong.setVisibility(View.GONE);
                    mMore.setImageResource(R.drawable.drop_down);
                }
                break;
        }
    }

    /**
     * 获取播放次数
     *
     */
    private void getPlayCounts(String mediaid, String submediaid) {
        LedimApiServiceFactory.createService(ApiService.class)
                              .getPlayCounts(mediaid, submediaid, mediaid, submediaid)
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseResultDataInfo<LedimPlayCountBean>>() {
                                 @Override
                                 public void onCompleted() {
                                     super.onCompleted();
                                 }

                                 @Override
                                 public void onError(Throwable e) {
                                     super.onError(e);
                                     LogUtil.e(e.getMessage());
                                 }

                                 @Override
                                 public void onNext(BaseResultDataInfo<LedimPlayCountBean> data) {
                                     super.onNext(data);
                                     if (data != null) {
                                         if (data.success) {
                                             //成功
                                             if (data.data.plist_play_count != 0) {
                                                 mNum.setText(
                                                         LedimFunctionUtils.formatGoldCount(data.data.plist_play_count) +
                                                                 "次观看");
                                             }
                                         } else {
                                             ToastUtil.toast(data.message);
                                         }
                                     }
                                 }
                             });
    }

}
